package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ClassNewsDAO;
import dao.ClassNewsDaoJdbcImpl;
import modle.classnews;

/**
 * Servlet implementation class NewsAddServlet
 */
@WebServlet("/NewsAddServlet")
public class NewsAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cNID = request.getParameter("cNID");
		String newsName = request.getParameter("newsName");
		String content = request.getParameter("content");
		String newsTime = request.getParameter("newsTime");
		String time = newsTime.substring(0, 9);

		ClassNewsDAO cnd = new ClassNewsDaoJdbcImpl();
		classnews classnews = null;
		if(cNID=="") { //为空时添加文章
			classnews = new classnews(0, newsName, newsTime, content);
			cnd.writeClassNews(classnews);
		}else  //编辑文章
		{
			classnews = new classnews(Integer.parseInt(cNID), newsName, newsTime, content);
			cnd.updateClassNews(classnews);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
